//
//  CMenuBar.mm
//  AudiHistory
//
//  Created by niko on 11-8-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CMenuBar.h"
#import "AudiHistoryViewController.h"
#import "CPageDataStruct.h"

@implementation CMenuBar

//- (id)initWithFrame:(CGRect)frame
//{
//    if ((self = [super initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]))
//	{
//		
//    }
//    return self;
//}
-(void)IndexBtn
{
	[[AudiHistoryViewController SharedObj] ShowIndexBar];
}

-(void)RightBtn
{
	int nPageCount = [[CPageDataStructTable SharedObj] GetPageCount];
	int nCurPage = [[AudiHistoryViewController SharedObj] GetCurNewPage];
	
	if ((nCurPage + 1) >= nPageCount)
	{
		return;
	}
	
	[[AudiHistoryViewController SharedObj] SetCurNewPage:nCurPage + 1];
}

-(void)LeftBtn
{
	int nCurPage = [[AudiHistoryViewController SharedObj] GetCurNewPage];
	
	if ((nCurPage + 1) <= 1)
	{
		return;
	}
	
	[[AudiHistoryViewController SharedObj] SetCurNewPage:nCurPage - 1];
}

-(void)UpdatePageNumLabel
{
	int nPageCount = [[CPageDataStructTable SharedObj] GetPageCount];
	int nCurPage = [[AudiHistoryViewController SharedObj] GetCurNewPage];
	
	if (nil == m_pPageNumLabel)
	{
		m_pPageNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width * 0.2, self.frame.size.height)];
		//[m_pPageNumLabel textRectForBounds:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) limitedToNumberOfLines:20];
		m_pPageNumLabel.textAlignment = UITextAlignmentCenter;
		m_pPageNumLabel.textColor = [UIColor whiteColor];
		m_pPageNumLabel.backgroundColor = [UIColor clearColor];
		m_pPageNumLabel.font = [UIFont boldSystemFontOfSize:17];
		
		[self insertSubview:m_pPageNumLabel aboveSubview:m_pMenuBar];
		
		m_pPageNumLabel.center = CGPointMake(self.frame.size.width * 0.5, m_pPageNumLabel.center.y);
	}
	
	NSString *strVal = [NSString stringWithFormat:@"%d    of    %d", nCurPage + 1, nPageCount];
	m_pPageNumLabel.text = strVal;
}

-(void)BuildMenuBar
{
	//加载下边条
	//m_pMenuBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height * 0.9, self.frame.size.width, self.frame.size.height * 0.1)];
	m_pMenuBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	[self addSubview:m_pMenuBar];
	m_pMenuBar.backgroundColor = [UIColor blackColor];
	m_pMenuBar.alpha = 0.8;
	
	[self UpdatePageNumLabel];
	
	//左按钮
	UIButton *btnBtnL = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *imageL = [[UIImage alloc] initWithContentsOfFile:fullPathFromRelativePath(@"leftBtn.png")];
	[btnBtnL setImage:imageL forState:UIControlStateNormal];
	btnBtnL.backgroundColor = [UIColor clearColor];
	btnBtnL.frame = CGRectMake(m_pPageNumLabel.center.x - 100 - imageL.size.width, m_pPageNumLabel.center.y - (imageL.size.height * 0.5), imageL.size.width, imageL.size.height);
	[btnBtnL addTarget:self action:@selector(LeftBtn) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:btnBtnL];
	//[self insertSubview:btnBtnL aboveSubview:m_pPageNumLabel];
	//btnBtnL.center = CGPointMake(m_pPageNumLabel.center.x - 100, btnBtnL.center.y);
	RELEASE_OBJECT(imageL);
	
	//右按钮
	UIButton *btnBtnR = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *imageR = [[UIImage alloc] initWithContentsOfFile:fullPathFromRelativePath(@"rightBtn.png")];
	[btnBtnR setImage:imageR forState:UIControlStateNormal];
	btnBtnR.backgroundColor = [UIColor clearColor];
	btnBtnR.frame = CGRectMake(m_pPageNumLabel.center.x + 100, m_pPageNumLabel.center.y - (imageR.size.height * 0.5), imageR.size.width, imageR.size.height);
	[btnBtnR addTarget:self action:@selector(RightBtn) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:btnBtnR];
	RELEASE_OBJECT(imageR);
	
	//index按钮
	UIButton *btnIndex = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *imageIndex = [[UIImage alloc] initWithContentsOfFile:fullPathFromRelativePath(@"IndexBtn.png")];
	[btnIndex setImage:imageIndex forState:UIControlStateNormal];
	btnIndex.backgroundColor = [UIColor clearColor];
	btnIndex.frame = CGRectMake(self.frame.size.width - imageIndex.size.width - 5, m_pPageNumLabel.center.y - (imageIndex.size.height * 0.5), imageIndex.size.width, imageIndex.size.height);
	[btnIndex addTarget:self action:@selector(IndexBtn) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:btnIndex];
	RELEASE_OBJECT(imageIndex);
}

-(void)Build
{
	[self BuildMenuBar];
}

-(void)Clear
{	
	[m_pMenuBar removeFromSuperview];
	RELEASE_OBJECT(m_pMenuBar);
	
	[m_pPageNumLabel removeFromSuperview];
	RELEASE_OBJECT(m_pPageNumLabel);
}

-(void)dealloc
{
	[self Clear];
	
	[super dealloc];
}

@end
